package com.hjxy.codetools.controller;

import com.hjxy.codetools.FreemarkerUtil;
import com.hjxy.codetools.model.ArtitleModel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class FreemarkController {
    @RequestMapping("/test")
    public String index(Model model) {
        List<ArtitleModel> artitleModels=new ArrayList<>();
        ArtitleModel artitleModel=new ArtitleModel();
        artitleModel.setRandcode("232121212");
        artitleModel.setContent("content");
        artitleModel.setIshot(true);
        artitleModel.setSlturl("66");
        artitleModel.setTitle("title");
        artitleModels.add(artitleModel);


        ArtitleModel artitleModel2=new ArtitleModel();
        artitleModel.setRandcode("232121212");
        artitleModel.setContent("content");
        artitleModel.setIshot(true);
        artitleModel.setSlturl("66");
        artitleModel.setTitle("title");
        artitleModels.add(artitleModel2);


        ArtitleModel artitleModel3=new ArtitleModel();
        artitleModel.setRandcode("232121212");
        artitleModel.setContent("content");
        artitleModel.setIshot(true);
        artitleModel.setSlturl("66");
        artitleModel.setTitle("title");
        artitleModels.add(artitleModel3);


        model.addAttribute("list", artitleModels);

        return "index";
    }


    @RequestMapping(value = "/api/{city}/{id}", method = RequestMethod.GET)
    public String findOneCity(Model model, @PathVariable("city") String city, @PathVariable("id") Integer id) {
        Map map = new LinkedHashMap<>();
        for (int i = 0; i < 5; i++) {
            map.put("key" + i, "value" + i);
        }
        model.addAttribute("list", Arrays.asList("string1", "string2", "string3", "string4", "string5", "string6"));
        model.addAttribute("map", map);
        model.addAttribute("name", "   htTps://wWw.zHyD.mE   ");
        model.addAttribute("htmlText", "<span style=\"color: red;font-size: 16px;\">html内容</span>");
        model.addAttribute("num", 123.012);
        model.addAttribute("null", null);
        model.addAttribute("dateObj", new Date());
        model.addAttribute("bol", true);
        return "index";
    }


    @RequestMapping("/createHtml")
    @ResponseBody
    public String createHtml(Model model){
        Map map = new LinkedHashMap<>();
        for (int i = 0; i < 5; i++) {
            map.put("key" + i, "value" + i);
        }
        model.addAttribute("list", Arrays.asList("string1", "string2", "string3", "string4", "string5", "string6"));
        model.addAttribute("map", map);
        model.addAttribute("name", "   htTps://wWw.zHyD.mE   ");
        model.addAttribute("htmlText", "<span style=\"color: red;font-size: 16px;\">html内容</span>");
        model.addAttribute("num", 123.012);
        model.addAttribute("null", null);
        model.addAttribute("dateObj", new Date());
        model.addAttribute("bol", true);
        return FreemarkerUtil.parseTpl("index", model.asMap());
    }


}
